package 华为OD机试真题2023.基础题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class HJ17_开心消消乐 {
    static int row;
    static int column;

    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/17.txt"));

        row = in.nextInt();
        column = in.nextInt();
        int[][] juzhen = new int[row][column];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                juzhen[i][j] = in.nextInt();
            }
        }
        int res = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (juzhen[i][j] == 1) {
                    clear(juzhen, i, j);
                    res++;
                }
            }
        }
        System.out.println(res);
    }

    static void clear(int[][] juzhen, int i, int j) {
        if (i < 0 || i >= row || j < 0 || j >= column || juzhen[i][j] != 1) return;
        juzhen[i][j] = 0;
        clear(juzhen, i - 1, j); //上
        clear(juzhen, i + 1, j); //下
        clear(juzhen, i, j - 1); //左
        clear(juzhen, i, j + 1); //右
        clear(juzhen, i - 1, j - 1); //左上
        clear(juzhen, i - 1, j + 1); //右上
        clear(juzhen, i + 1, j - 1); //左下
        clear(juzhen, i + 1, j + 1); //右下
    }
}
